package com.atguigu.singleton.type7;

/**
 * @Description: 静态内部类方式 推荐
 * @Author lixiaolong
 * @Date 2023/9/26
 */
public class SingletonTest07 {

	public static void main(String[] args) {
		Singleton instance = Singleton.getInstance();
		Singleton instance2 = Singleton.getInstance();
		System.out.println(instance == instance2); // true
		System.out.println("instance.hashCode=" + instance.hashCode());
		System.out.println("instance2.hashCode=" + instance2.hashCode());
		
	}

}

class Singleton {
	private static volatile Singleton instance;
	
	private Singleton() {}
	
	private static class SingletonInstance {
		private static final Singleton INSTANCE = new Singleton(); 
	}

	public static synchronized Singleton getInstance() {
		
		return SingletonInstance.INSTANCE;
	}
}